package com.tot.io;

//import org.apache.commons.lang.ArrayUtils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by TOT on 2016/5/26.
 */
public class StreamDemo {
//    static void fromFileStream() throws Exception{
//        DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(new FileInputStream("C:\\Users\\TOT\\Documents\\SVN_OSCHINA\\plugindog\\src\\main\\resources\\新建文本文档.txt")));
//        String temp,inString = new String();
////        int c  ;
////        while ( (c = dataInputStream.read() ) != -1){
////            System.out.print((char) c);
////        }
//        while ((temp = dataInputStream.readLine()) != null){
//            inString += temp;
//        }
//        System.out.println("\n"+inString);
//    }
//
//    static void readFromFileByReader() throws Exception {
//        BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\TOT\\Documents\\SVN_OSCHINA\\plugindog\\src\\main\\resources\\新建文本文档.txt"));
//        String temp,inString = new String();
//
//        while ((temp = bufferedReader.readLine()) != null){
//            inString += temp;
//        }
//
//
//        System.out.println(inString);
//    }
//
//    static void inputStreamTest() throws Exception {
//        byte[] sources = "12345678aA".getBytes();
//        System.out.print(Arrays.toString(sources));
//        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(sources);
//        int i = 0;
//        while(( i = byteArrayInputStream.read()) != -1){
//            System.out.println(Integer.toBinaryString(i));
//        }
////        int byteArrayInputStream.read();
////        byteArrayInputStream.
//    }
//
//    //测试 FileInputStream , FileOutputStream
//    static void fileStreamTest() throws IOException {
//        File file = new File("fileStream.dat");
//        FileOutputStream fileOutputStream = new FileOutputStream(file);
//        fileOutputStream.write("中文测试，abc test".getBytes("UTF-8"));
//        fileOutputStream.flush();
//        FileInputStream fileInputStream = new FileInputStream(file);
//        int c = 0;
//        System.out.println("the read result==>");
//        List<Byte> bytes = new ArrayList<>();
//        while ((c = fileInputStream.read()) != -1){
//            bytes.add((byte)c);
//        }
//        Byte[] byteArray = bytes.toArray(new Byte[]{});
//
////        byte[] finalByteArray = (byte[])byteArray;
//        byte[] bytes1 = ArrayUtils.toPrimitive(byteArray);
//        String result = new String(bytes1,"UTF-8");
//        System.out.println("==reuslt===>"+result);
//
//    }
//
//
//    // ByteArrayOutputStream ByteArrayInputStream测试
//
//    static void testByteStream() throws IOException {
//        /**  This class implements an output stream in which the data is
//        * written into a byte array. The buffer automatically grows as data
//        * is written to it. **/
//        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//        //强字节数组写入outputStream的buffer中去。
//        outputStream.write("中文测试 abc test".getBytes("UTF-8"));
//        //查看buffer内容
//        System.out.println("buffer to string"+outputStream.toString("UTF-8"));
//        System.out.println("bytes[]===>"+Arrays.toString(outputStream.toByteArray()));
//
//        //测试inputStream
//        /**
//         *  * A <code>ByteArrayInputStream</code> contains
//         * an internal buffer that contains bytes that
//         * may be read from the stream. An internal
//         * counter keeps track of the next byte to
//         * be supplied by the <code>read</code> method.
//         */
//        byte[] bytes = "这是真正的读取源 abc".getBytes("UTF-8");
//        System.out.println("byte length"+bytes.length);
//        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
//        int c = 0;
//        List<Byte> byteList = new ArrayList<>();
//        while ((c = byteArrayInputStream.read()) != -1){
//            byteList.add((byte)c);
//        }
//        Byte[] byteArray = byteList.toArray(new Byte[]{});
//
////        byte[] finalByteArray = (byte[])byteArray;
//        byte[] bytes1 = ArrayUtils.toPrimitive(byteArray);
//        String result = new String(bytes1,"UTF-8");
//        System.out.println("==reuslt===>"+result);
//    }
//
//    static void pipedStreamTest() throws IOException {
//
//        /**
//         * A piped input stream should be connected
//         * to a piped output stream; the piped  input
//         * stream then provides whatever data bytes
//         * are written to the piped output  stream.
//         * Typically, data is read from a <code>PipedInputStream</code>
//         * object by one thread  and data is written
//         * to the corresponding <code>PipedOutputStream</code>
//         * by some  other thread. Attempting to use
//         * both objects from a single thread is not
//         * recommended, as it may deadlock the thread.
//         * The piped input stream contains a buffer,
//         * decoupling read operations from write operations,
//         * within limits.
//         * A pipe is said to be <a name="BROKEN"> <i>broken</i> </a> if a
//         * thread that was providing data bytes to the connected
//         * piped output stream is no longer alive.
//         */
//        PipedInputStream  pipedInputStream = new PipedInputStream();
//        PipedOutputStream pipedOutputStream = new PipedOutputStream(pipedInputStream);
//        pipedOutputStream.write("hello PipedInputStream".getBytes());
//        int c = 0;
//        while ((c = pipedInputStream.read()) != -1){
//            System.out.print((char)c);
//        }
//        System.out.println("closed==>");
//        pipedInputStream.close();
//        pipedOutputStream.close();
//    }
//
//    static void filterStreamTest() throws IOException{
//        FileOutputStream fileOutputStream = new FileOutputStream("fliterStream.dat");
//        //FilterInputStream的子类
//        DataOutputStream dataOutputStream = new DataOutputStream(fileOutputStream);
//        //写入操作
//        dataOutputStream.writeInt(12345);
//        dataOutputStream.writeBoolean(true);
//        dataOutputStream.writeChar('A');
//
//        FileInputStream fileInputStream = new FileInputStream("fliterStream.dat");
//        //FilterInputStream的子类
//        DataInputStream dataInputStream = new DataInputStream(fileInputStream);
//        //读取操作
//        int num = dataInputStream.readInt();
//        System.out.println("the int num==>"+num);
//        System.out.println("the boolean result==>" + dataInputStream.readBoolean());
//        System.out.println("the char result==>" + dataInputStream.readChar());
//
//    }
//
////    static void printStreamDemo(){
////        PrintStream
////    }
//
//
//    public static void main(String[] args) throws Exception {
////        Integer i = new Integer("");
////        fromFileStream();
////        readFromFileByReader();
////        inputStreamTest();
////        fileStreamTest();
////        testByteStream();
////        pipedStreamTest();
//        filterStreamTest();
//    }
}
